package com.warm.androidstudy.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Rect;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

/**
 * Created by Administrator on 2017/9/20.
 */

public class BitmapUtil {

    public BitmapUtil() {
    }

    //在不加载图片情况下获取图片大小(网络url)
    public static int[] getImageWidthHeight(final String url) {
        try {
            final Options options = new Options();
            /**
             * 最关键在此，把options.inJustDecodeBounds = true;
             * 这里再decodeFile()，返回的bitmap为空，但此时调用options.outHeight时，已经包含了图片的高了
             */
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new URL(url).openStream(), null, options);
            int width = options.outWidth;
            int height = options.outHeight;
            return new int[]{width, height};
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }   

    public static Bitmap decodeFileToBitmap(String iamgePath) {
        Bitmap bitmap = null;
        Options bfOptions = new Options();
        bfOptions.inTempStorage = new byte[4194304];
        bfOptions.inDither = false;
        bfOptions.inPurgeable = true;
        File file = new File(iamgePath);
        FileInputStream fs = null;

        try {
            fs = new FileInputStream(file);
        } catch (FileNotFoundException var16) {
            LogUtil.e("无法找到图片文件：" + iamgePath);
        }

        if (fs != null) {
            try {
                bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(), (Rect) null, bfOptions);
            } catch (IOException var15) {
                var15.printStackTrace();
                LogUtil.e(var15.getMessage());
            } finally {
                if (fs != null) {
                    try {
                        fs.close();
                    } catch (IOException var14) {
                        var14.printStackTrace();
                    }
                }

            }
        }

        return bitmap;
    }

    public Bitmap getBitmapFromFile(File dst, int width, int height) {
        if (null != dst && dst.exists()) {
            Options opts = null;
            if (width > 0 && height > 0) {
                opts = new Options();
                opts.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(dst.getPath(), opts);
                int e = Math.min(width, height);
                opts.inSampleSize = computeSampleSize(opts, e, width * height);
                opts.inJustDecodeBounds = false;
                opts.inInputShareable = true;
                opts.inPurgeable = true;
            }

            try {
                return BitmapFactory.decodeFile(dst.getPath(), opts);
            } catch (OutOfMemoryError var6) {
                var6.printStackTrace();
            }
        }

        return null;
    }

    public static int computeSampleSize(Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            for (roundedSize = 1; roundedSize < initialSize; roundedSize <<= 1) {
                ;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }

        return roundedSize;
    }

    private static int computeInitialSampleSize(Options options, int minSideLength, int maxNumOfPixels) {
        double w = (double) options.outWidth;
        double h = (double) options.outHeight;
        int lowerBound = maxNumOfPixels == -1 ? 1 : (int) Math.ceil(Math.sqrt(w * h / (double) maxNumOfPixels));
        int upperBound = minSideLength == -1 ? 128 : (int) Math.min(Math.floor(w / (double) minSideLength), Math.floor(h / (double) minSideLength));
        return upperBound < lowerBound ? lowerBound : (maxNumOfPixels == -1 && minSideLength == -1 ? 1 : (minSideLength == -1 ? lowerBound : upperBound));
    }
}
